<%--
  Created by IntelliJ IDEA.
  User: lujw
  Date: 2017/6/15
  Time: 9:55
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ include file="/common/hospital_token.jsp"%>
<%@ include file="/common/hospital_notice.jsp"%>
<%!
    /**
     * 我发布的通知(分页)
     * @param token
     * @return
     */
    public Message myPublish(HospitalToken token, Input input) {
        Integer page = 1; // 页码
        Integer itemCount = 20; // 每一页条目
        Integer hospital = input.getInt("hospital", -1);
        //通知id
        Integer noticeId = input.getInt("notice_id", -1);
        //通知类别
        Integer category = input.getInt("category", -1);
        //通知发送时间
        String sendTime = input.getDesc("send_time", "");
        //refresh下拉刷新，load上拉加载
        String act = input.getDesc("act", "refresh");
        String selectContent = "new map(n.id as id, n.operatorId as operator_id, n.title as title, n.content as content, n.isComment as is_comment, n.author as author, n.attachment as attachment, concat(from_unixTime(n.cTime/1000, '%Y-%m-%d %H:%i:%s'), '') as time, c.name as category_name, l.name as level_name, l.jsonString as json)";
        Where where = new Where("n.status=", ConstantUtil.STATUS_ENABLE).and("n.operatorId=", token.getUid()).and("n.hospital=", hospital).and("c.status=", ConstantUtil.STATUS_ENABLE).and("c.hospital=", NOTICE_CATEGORY_HOSPITAL).and("l.status=", ConstantUtil.STATUS_ENABLE).and("l.hospital=", hospital).and("n.fromClient='app'").and("n.category=c.id").and("n.level=l.id");
        if (category != -1)
            where.and("c.id=", category);
        String order = "";
        if ("refresh".equals(act)) {
            order = "asc";
            if (!"".equals(sendTime) && noticeId != -1)
                where.and("from_unixTime(n.cTime/1000, '%Y-%m-%d %H:%i:%s') > '"+sendTime + "' or (from_unixTime(n.cTime/1000, '%Y-%m-%d %H:%i:%s') = '" + sendTime + "' and n.id > " + noticeId + ")");
        } else {
            order = "desc";
            if (!"".equals(sendTime) && noticeId != -1)
                where.and("from_unixTime(n.cTime/1000, '%Y-%m-%d %H:%i:%s') < '"+sendTime + "' or (from_unixTime(n.cTime/1000, '%Y-%m-%d %H:%i:%s') = '" + sendTime + "' and n.id < " + noticeId + ")");
        }
        List<Map> list = QBuilder.db("basedb").select(selectContent).from("HospitalNotice n, HospitalNoticeCategory c, HospitalNoticeLevel l").where(where).order("n.cTime desc, n.id ", order).page(itemCount, page).query().getResultObj();
        if ("refresh".equals(act)) {
            Collections.sort(list, new Comparator<Map>() {
                @Override
                public int compare(Map o1, Map o2) {
                    Date firstDate = DateUtil.parseDateTime(Tools.getDesc(o1.get("time")));
                    Date lastDate = DateUtil.parseDateTime(Tools.getDesc(o2.get("time")));
                    Integer dateCompareResult = Objects.isNull(lastDate) ? -1 : Objects.isNull(firstDate) ? 1 : lastDate.compareTo(firstDate);
                    if (dateCompareResult == 0) {
                        Integer firstId = (Integer)o1.get("id");
                        Integer lastId = (Integer)o2.get("id");
                        return Integer.compare(lastId, firstId);
                    }
                    return dateCompareResult;
                }
            });
        }
        formatNoticeListResult(list, hospital);
        Message msg = new Message();
        msg.put("data", list);
        return msg;
    }
%>
<%
    Tools.encryptPrint(out, myPublish(token, input));
%>
